
const zbTree = function () {
    "use strict";
    this.sidebarId = '';
    this.sidebar = null;
    this.openChar = '+';
    this.closeChar = '-';
    this.toggleCharLength = 1;
    this.dirClassName = 'dir';
    this.fileClassName = 'file';

    this.init = function (id, option) {
        this.sidebarId = id;
        this.sidebar = document.getElementById(this.sidebarId);

        (option.openChar !== undefined) && (this.openChar = option.openChar);
        (option.closeChar !== undefined) && (this.closeChar = option.closeChar);
        (option.dirClassName !== undefined) && (this.dirClassName = option.dirClassName);
        (option.fileClassName !== undefined) && (this.fileClassName = option.fileClassName);

        if (this.openChar.length === this.closeChar.length) {
            this.toggleCharLength = this.closeChar.length;
        } else if (this.openChar.length > this.closeChar.length) {
            this.closeChar = this.closeChar.padEnd(this.openChar.length, ' ');
            this.toggleCharLength = this.openChar.length;
        } else {
            this.openChar = this.openChar.padEnd(this.closeChar.length, ' ');
            this.toggleCharLength = this.closeChar.length;
        }

        let dirs = this.sidebar.getElementsByClassName(this.dirClassName);
        let that = this;
        for (let i=0; i<dirs.length; i++) {
            (function (i) {
                dirs[i].onclick = function () {
                    that.toggle(dirs[i].getAttribute('id'))
                }
            })(i);
        }

        let files = this.sidebar.getElementsByClassName(this.fileClassName);
        for (let i=0; i<files.length; i++) {
            (function (i) {
                files[i].onclick = function () {
                    that.redirect(files[i].getAttribute('id'))
                }
            })(i);
        }
    }

    // this.sidebar.onclick =
    //显示目录
    this.showSider = function () {
        let windowWidth = window.screen.width;

        // this.sidebar.style.width = windowWidth;

        if (this.sidebar.style['margin-left'] === '0px') {
            this.sidebar.style['margin-left'] = '-'+windowWidth+'px';
        } else {
            this.sidebar.style['margin-left'] = '0px';
        }
    }

    //隐藏目录
    this.hideSider = function (minWidth) {
        let windowWidth = window.screen.width;
        if (windowWidth < minWidth) {
            this.sidebar.style.width = windowWidth;
            this.sidebar.style['margin-left'] = '-'+windowWidth+'px';
        }
    }

    //关闭所有非一级目录
    this.closeAll = function () {
        let sidebar = this.sidebar;
        let tagP = sidebar.getElementsByTagName('p');

        for (let i=0; i<tagP.length; i++) {
            let clsses = this.getClass(tagP[i]);
            let isDir = (clsses['dir'] !== undefined);
            let isFile = (clsses['file'] !== undefined);
            let level = 0;
            for (let cls in clsses) {
                if (cls.indexOf('l-') !== -1) {
                    level = cls.split('-')[1];
                    break;
                }
            }

            if (parseInt(level) === 1 && isDir) {
                tagP[i].innerText = this.openChar + tagP[i].innerText.substring(this.toggleCharLength).replace(/\s+/, ''); //一级目录, 将"-" 变为 "+", 不隐藏
            }

            if (parseInt(level) > 1 && isDir) {
                tagP[i].innerText = this.openChar + tagP[i].innerText.substring(this.toggleCharLength).replace(/\s+/, ''); //二级目录, 将"-" 变为 "+", 隐藏
                tagP[i].hidden = true;
                this.slow();
            }

            if (parseInt(level) > 1 && isFile) {
                tagP[i].hidden = true; //文件, 隐藏
                this.slow();
            }
        }
    }

    //展开到某个节点
    this.openTo = function (code, node='') {
        this.closeAll();

        let chain = null;
        for (let j=0; j<classChain.length; j++) {
            if (classChain[j][code] !== undefined) {
                chain = classChain[j];
                break;
            }
        }

        for (let id in chain) {
            let element = document.getElementById(id);
            element.innerText = this.closeChar + element.innerText.substring(this.toggleCharLength).replace(/\s+/, '');
            let elements = document.getElementsByClassName(id);
            let hidden = true;
            for (let k=0; k<elements.length; k++) {
                elements[k].hidden = false;
            }

            if (code === id) {break;}
        }

        if (node.length !== undefined && node.length > 0) {
            document.getElementById(node).style.fontWeight = 'bold';
        }
    }

    //展开或折叠
    this.toggle = function (id) {
        let obj = document.getElementById(id);
        let curPath = obj.getAttribute('id');
        let prefix = obj.innerText.substring(0,this.toggleCharLength);
        let title = obj.innerText.substring(this.toggleCharLength).replace(/\s+/, '');
        let hidden = (prefix === this.closeChar); //如果当前点击时, 前缀是-, 意思是要展开下一级

        //先把所有节点都隐藏, 然后打开本节点
        this.openTo(curPath);

        if (hidden === true) {
            obj.innerText = this.openChar + title;
            let childs = document.getElementsByClassName(curPath);
            for (let i=0; i<childs.length; i++) {
                childs[i].hidden = true;
                this.slow();
            }
        }
    }

    //解析查询参数成对象
    this.parseQuery = function (query) {
        let pos = query.indexOf('?');
        if (pos === -1) {
            return {};
        }

        query = query.substring(pos + 1);
        query = decodeURIComponent(query);

        if (query.length === 0) {
            return {};
        }

        let items = null, item = null, name = null, value = null;

        if (query.indexOf('&') === -1) {
            items = query.split("=");
            name = items[0];
            value = items[1];
            let tmp = {};
            tmp[name] = value;
            return tmp;
        }

        let args = {};
        items = query.split("&");
        for(let i=0; i < items.length; i++){
            item = items[i].split("=");
            if(item[0]){
                name = item[0];
                value = item[1] ? item[1] : "";
                args[name] = value;
            }
        }

        return args;
    }

    //跳转页面
    this.redirect = function (id) {
        let obj = document.getElementById(id);
        if (obj.getAttribute('data-uri') !== null) {
            location.href = location.origin + '/' + obj.getAttribute('data-uri');
        }
    }

    //获取class属性
    this.getClass = function (elem) {
        let attr = elem.getAttribute('class');
        let arr = attr.replace(/\s+/g, ' ').split(' ');
        let rs = {};
        for (let i=0; i<arr.length; i++) {
            rs[arr[i]] = '1';
        }

        return rs;
    }

    //判断是否存在某个属性
    this.hasClass = function (elem, cls) {
        let obj = getClass(elem);
        return (obj[cls] !== undefined);
    }

    //减缓折叠速度
    this.slow = function () {
        let sleep=0;
        for (let k=0; k<20000; k++) {
            sleep++;
        }
    }

    //将目录绑定事件
    this.bindDir = function () {

    }
    //将文件绑定事件
    this.bindFild = function () {

    }

}